'use strict'

var express = require('express')
var config = require('./config/index')
var app = express()
var port = process.env.PORT || config.build.port


var router = express.Router()

router.get('/', function(req, res, next) {
	req.url = '/index.html'

	console.log(req.url)
	next()
})

var appData = require('./data.json')
var goods = appData.goods
var seller = appData.seller
var ratings = appData.ratings

var appRouter = express.Router()

appRouter.get('/goods', function(req, res) {
  res.json({
    errno: 0,
    data: goods
  })
})
appRouter.get('/seller', function(req, res) {
  res.json({
    errno: 0,
    data: seller
  })
})
appRouter.get('/ratings', function(req, res) {
  res.json({
    errno: 0,
    data: ratings
  })
})

// index 
app.use(router)
// api路由
app.use('/api', appRouter)
// 静态资源目录
app.use(express.static('./dist'))

// listen
module.exports = app.listen(port, function(err) {
	if(err) {
		console.log(err) 
		return
	}

	console.log('Listening at http://localhost:' + port + '\n')
})